package input;

import instance.Instance;
import instance.InstanceEj2;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.util.LinkedList;
import java.util.StringTokenizer;

import problem.ej2.Ejes_strings;



public class Problem2Input implements Input{
	
	
	private String inputFile;
	private LinkedList<Instance> instances;
	
	public Problem2Input(String inputFile) {
		this.inputFile = inputFile;

	}
    //nos guardamos todas las instancias del archivo de entrada para procesarlos de a uno
	
	@Override
	public void readFile() throws Exception {
				
		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				new File(inputFile)));

		instances = new LinkedList<Instance>();

		while (bufferedReader.ready()) {
			Instance instance =  readInstance(bufferedReader);
			instances.add( instance);
		}
		
		bufferedReader.close();	
	}

	@Override
	public LinkedList<Instance> getInstances() {
	
		return instances;
	}

	@Override
	public Instance readInstance(BufferedReader bufferedReader)
			throws Exception {

		LinkedList <Ejes_strings> instance = new LinkedList<Ejes_strings>();
		InstanceEj2 subInstance ;
		String line = bufferedReader.readLine();
		StringTokenizer p_y_q = new StringTokenizer(line);
		Ejes_strings ts_pyq = new Ejes_strings();
		ts_pyq.inv_1 = p_y_q.nextToken().trim();
		ts_pyq.inv_2 = p_y_q.nextToken().trim();
		
		line = bufferedReader.readLine();
		StringTokenizer tokens = new StringTokenizer(line, ";");
		
		while(tokens.hasMoreTokens()){
			String tup = tokens.nextToken().trim();
			StringTokenizer tupla =  new StringTokenizer(tup);

			String i1 =tupla.nextToken().trim();
			String i2= tupla.nextToken().trim();

			Ejes_strings eje = new Ejes_strings(i1,i2);
			instance.add(eje); 
		}
		subInstance = new InstanceEj2(instance, ts_pyq);

				
		return subInstance;
		
		
		
		
		
	}
		

}
